<template>
	<div class='home-div'>
		<!--顶部新建包裹菜单-->
		<div class='header-div'>
			<div class='list'>
				<navigator url="/pages/newpackage/index">
					<span class='iconfont icon-baoguo'></span>
					<p class='txt'>新建包裹</p>
				</navigator>
			</div>
			<div class='list'>
				<span class='iconfont icon-saoyisao' @click='scanQrcode'></span>
				<p class='txt'>扫一扫</p>
			</div>
			<div class='list'>
				<span class='iconfont icon-erweima' @click='orderToMe'></span>
				<p class='txt'>下单给我</p>
			</div>
		</div>
		
		<MyRight />
	</div>
</template>

<script>
import MyRight from '@/components/myright.vue';
import {showToast,post} from '../../utils/index';
import {mapGetters,mapActions} from 'vuex';
import {getSendPackageToUrl,addMyPartnerUrl} from '../../config.js'
export default {
	data() {
		return {
			
		};
	},
	computed: {
		...mapGetters(['userInfo'])
	},
	components: {
		MyRight
	},
	mounted() {
		this.getSendPackageToFunc()
	},
	methods: {
		//获取设置的默认将包裹提交给谁
		getSendPackageToFunc(){
			var self = this;
			post(getSendPackageToUrl, {
				session_token: self.userInfo.session_token
			})
			.then((res)=>{
				if(res.code==200){
					self.$store.dispatch('setSendPackageTo', res.data);
				}else{
					showToast(res.errMsg);
				}
			});
		},
		//扫描二维码
		scanQrcode(){
			var self = this;
			uni.scanCode({
				scanType: ['qrCode'],    //只允许扫描二维码
				success(res){
					let result = res.result;  //所扫码的内容
					try{
						result = JSON.parse(result);
						/**可以得到被扫码人的信息
						* {
							id,name,img,以及被扫码人的寄件地址
						* }
						* **/
						let uid = result.uid;  //被扫用户的uid
						//扫码后将其设置为我的小伙伴
						post(addMyPartnerUrl, {
							session_token: self.userInfo.session_token,
							id: uid  //伙伴的id
						})
						.then((res)=>{
							if(res.code==200){
								//这里还需要处理寄件人信息
								uni.navigateTo({
									url: '/pages/newpackage/index?uid='+uid
								});
							}else{
								showToast(res.errMsg);
							}
						});
					}
					catch(e){
						showToast('请扫正确的二维码');
					};
				},
				fail(){
					showToast('扫码失败');
				}
			});
		},
		//下单给我
		orderToMe(){
			uni.navigateTo({
				url: '/pages/ordertome/index'
			});
		}
	},
	onShareAppMessage() {
		
	}
}
</script>

<style lang="less">
@import '../../style/default.less';

/**头部菜单**/
.header-div{
	height: 150upx;
	background: @mainColor;
	display: flex;
	align-items: center;
	justify-content: space-around;
	color: #fff;
	.list{
		text-align: center;
		.iconfont{
			font-size: 80upx;
		}
		p.txt{
			line-height: initial;
			margin:0;
			padding: 0;
		}
	}
}
</style>
